<?php
session_start();
require_once '../header.php';
function __autoload($className)
	{
    	require_once('../class/' . $className . '.class.php');
	}
session::need_member();
$Plan=$_SESSION["plan"];
unset ($_SESSION["plan"]);
$UserId=session::getuserid();
//$CanBuy=user::need_CanBuy($UserId);
$CanBuy=TRUE;
if(empty($UserId))
{
	$CanBuy==FALSE;
}
if($CanBuy==FALSE)
{
	$_SESSION["erro"]="System Erro, plz relogin and buy again";
	redirect::location("../membercp.php");
	unset($_SESSION["cc"]);
	unset($_SESSION["ccid"]);
	unset($_SESSION["moneyplus"]);

}
else 
{
$type="cc";
if($Plan=="our")
{
	$MoneyPlus=$_SESSION["moneyplus"];
	$TotalPlus=count($_SESSION["ccid"])*$MoneyPlus;
	$CcIdList=$_SESSION["ccid"];
	foreach($CcIdList as $ccArr)
	{
		$arrId[]=$ccArr;
	}
	$Condition=database::BuildCondition(array(
	'ccid' => $arrId
	)
	);
	$MoneyPriceTemp=database::LimitQuery('ccinfo',array(
	'select' => 'sum(price)',
	'condition' => $Condition
	));
	$MoneyPrice=$MoneyPriceTemp[0]['sum(price)']+$TotalPlus;
	$MoneyComplete=round($MoneyPrice,2);
	$CheckMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $MoneyComplete");
	if($CheckMoney==1)
	{
		$UpdateMoney=database::Update('userinfo',array(
		'userid' => $UserId
		),array('money' => array( "money - {$MoneyComplete}" ))
		);
		$InsertOrder=user::insertorder($UserId, $MoneyComplete, 'our','cc');
		foreach($_SESSION["ccid"] as $GetCcId)
		{
			$CcId=$GetCcId;
			$GetPrice=database::LimitQuery('ccinfo',array(
			'select' => 'price',
			'condition' => array(
			'ccid' => $CcId
			)
			));
			$Price=$GetPrice[0]["price"];
			$PriceSell=$Price+$MoneyPlus;
			$UpdateCc=database::Update('ccinfo',array(
			'ccid' => $CcId
			),array(
			'pricesell' => $PriceSell,
			'orderid' => $InsertOrder,
			'userid' => $UserId,
			'site' => 'our'
			));
			
		}
		$_SESSION["erro"]="Thank You, Your order has been completed";
		redirect::location("../orderhistory.php");
		
	}
	else
	{
		$_SESSION["erro"]="You Do not have enough money";
		unset($_SESSION["cc"]);
		redirect::location("../lr/deposit.php");
	}
	unset($_SESSION["countrybuy"]);
	
}
else if($Plan=="codesell")
{
	echo "<img src='../images/loading.gif' />";
	$post=$_POST;
	$country=$_SESSION["countrybuy"];
	$moneyPlus=$_SESSION["moneyplus"];
	$price=$_SESSION["pricecodesell"];
	$quatity=rtrim(trim($post["freshcvvquatity"]));
	$quatity=intval($quatity);
	$TotalMoney=$quatity*$price;
	$typeCc=$_SESSION["typecc"];
	$binCc=$_SESSION["bincc"];
	$dob=$_SESSION["dob"];
	$getBalance=codesell::balanceCodeSell();
	if($TotalMoney>$getBalance)
	{
		$_SESSION["erro"]="Our System has some problem, plz choose another plan";
		redirect::location("../membercp.php");
	}
	else
	{
		$CheckMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $TotalMoney");
		if($CheckMoney==1)
		{
			$UpdateMoney=database::Update('userinfo',array(
			'userid' => $UserId
			),array('money' => array( "money - {$TotalMoney}" ))
			);
			$InsertOrder=user::insertorder($UserId, $TotalMoney, $Plan,$type);
			$Orderid=buycc::GetMaxId("orders","orderid", "userid=$UserId");
			$searchCc=codesell::checkoutCodesell($typeCc, $country, $binCc, $dob, $quatity, $UserId, $Orderid, $price);
			if($searchCc=="erro")
			{
				$_SESSION["erro"]="Our System has some problem, plz choose another plan";
				redirect::location("../membercp.php");
			}
			else
			{
				$_SESSION["erro"]="Thank You, Your order has been completed";
				redirect::location("../orderhistory.php");
			}
		}
		
	
	else
	{
		$_SESSION["erro"]="You Do not have enough money";
		redirect::location("../lr/deposit.php");
		unset($_SESSION["countrybuy"]);
		unset($_SESSION["moneyplus"]);
		unset($_SESSION["pricecodesell"]);
	}
	}
	
}
else
{
	$Country=$_SESSION["countrybuy"];
	$MoneyPlus=$_SESSION["moneyplus"];
	$CcIdList=$_SESSION["ccid"];
	$Quality=count($CcIdList);
	$Money=0;
	foreach($CcIdList as $cc)
	{
		$arrayCcId[]=$cc;
		$CcId=$cc;
		$CcNumber=$_SESSION["cc"][$CcId]["ccnum"];
		$TypeFind=substr($CcNumber,0,1);
		$GetArrPrice=database::LimitQuery('agent_price',array(
		'select' => 'agent_price',
		'condition' => array(
		'agent_name' => $Plan,
		'country' => $Country
		)
		));
		if(empty($GetArrPrice))
		{
			$getPrice=database::LimitQuery('agent_price',array(
			'select' => 'agent_price',
			'condition' => array(
			'agent_name' => $Plan,
			'country' => 'all'
			)
			));
			$priceTemp=$getPrice[0]['agent_price'];
			if(empty($priceTemp))
			{
				$priceTemp=buycc::returnPrice($CcId);
			}
		}
		else
		{
			$priceTemp=$GetArrPrice[0]['agent_price'];
		}
		$price=$priceTemp+$MoneyPlus;
		$Money=$price+$Money;
	}
	
	$TotalMoney=$Money;
	if($TotalMoney=="0")
	{
		$_SESSION["erro"]="plz try again";
		redirect::location("../membercp.php");
	}
	else
	{
	$CheckMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $TotalMoney");
	if($CheckMoney==1)
	{
		$getBalance=0;
		if($Plan=='ltd')
		{
			$getBalance=ltdcc::CheckBalanceLtd();
		}
		else if($Plan=='vmarket')
		{
			$getBalance=vmarket::checkBalanceVmarket();
		}
		else if($Plan=='partner')
		{
			$getBalance="1000";
		}
		else if($Plan=="cardcf")
		{
			$getBalance=cardcafe::getBalanceCardCafe();
		}
		else if($Plan=="mrhieu")
		{
			$getBalance=1000;
		}
		else 
		{
			$getBalance=1000;
		}
		$moneySee=$TotalMoney/2;
		if($getBalance<$moneySee)
		{
				$_SESSION["erro"]="This Agent has some problem, plz choose another agent";
				redirect::location("../membercp.php");
				
		}
		
		else
		{
			
			$UpdateMoney=database::Update('userinfo',array(
			'userid' => $UserId
			),array('money' => array( "money - {$TotalMoney}" ))
			);
			$InsertOrder=user::insertorder($UserId, $TotalMoney, $Plan,$type);
			$Orderid=buycc::GetMaxId("orders","orderid", "userid=$UserId");
			$erro=0;
			if($Plan=='ltd')
			{
					$BuyCc=ltdcc::CheckOut($CcIdList);
					if($BuyCc==FALSE)
					{
						$erro=1;
						
					}
					else 
					{
					
						$GetCc=ltdcc::GetCC($Quality, $price, $UserId, $Orderid,$Country);
						$erro="0";
					}
			}
			else if($Plan=="cardcf")
			{
				$BuyCc=cardcafe::checkOutCardCafe($CcIdList, $Quality, $price, $UserId, $Orderid);
			}
			else if($Plan=="mrhieu")
			{
				$BuyCc=mrhieucc::checkOutMrHieu($arrayCcId, $price, $UserId, $Orderid);
			}
			else if($Plan=="keyvnc")
			{
				$BuyCc=keyvnc::checkOutMrHieu($arrayCcId, $price, $UserId, $Orderid);
			}
			else if($Plan=="vnwcus")
			{
				$BuyCc=Vnwcus::checkOut($arrayCcId, $price, $UserId, $Orderid);
			}
			else if($Plan=="dimitry")
			{
			
				$BuyCc=dimitry::checkOutDimitry($arrayCcId, $price, $UserId, $Orderid);
			}
			else if($Plan=='vmarket')
			{
					$BuyCc=vmarket::checkOutVmarket($CcIdList,$Quality, $Country, $price, $UserId,$Orderid);
					if($BuyCc==100)
					{
						$erro=2;
						$UpdateMoney=database::Update('userinfo',array(
						'userid' => $UserId,
						),array('money' => array( "money + {$TotalMoney}" ))
						);
						$UpdateMoney=database::Update('orders',array(
						'userid' => $UserId,
						'orderid' => $Orderid
						),array('refund' => $TotalMoney)
						);
					}
					else
					{
						$refundCount=intval($BuyCc);
						if($refundCount<=$Quality)
						{
							$erro=0;
							$moneyRefund=ceil($refundCount*$price);
							$UpdateMoney=database::Update('userinfo',array(
							'userid' => $UserId
							),array('money' => array( "money + {$moneyRefund}" ))
							);
							$UpdateMoney=database::Update('orders',array(
							'userid' => $UserId,
							'orderid' => $Orderid
							),array('refund' => $moneyRefund)
							);
						}
						else 
						{
							$erro=3;
						}
						
					}
				
			}
			else if($Plan=="partner")
			{
				$BuyCc=reseller::checkoutReseller($CcIdList, $price, $UserId, $Orderid);
			}
			
			unset($_SESSION["countrybuy"]);
				unset($_SESSION["cc"]);
				unset($_SESSION["ccid"]);
				unset($_SESSION["moneyplus"]);
			if($erro==1)
			{
				$_SESSION["erro"]="Our System has some problem, plz choose another plan";
				redirect::location("buycc.php");
			}
			else if($erro==2)
			{
				$_SESSION["erro"]="Invalid Cc";
				redirect::location("../membercp.php");
			}
			else if($erro==0)
			{
				$_SESSION["erro"]="Thank You, Your order has been completed";
				redirect::location("../orderhistory.php");
			}
			else if($erro==3)
			{
				$_SESSION["erro"]="System Erro";
				redirect::location("../membercp.php");
			}

		}
		
		
	}
	else
	{
		$_SESSION["erro"]="You Do not have enough money";
		unset($_SESSION["cc"]);
		unset($_SESSION["ccid"]);
		redirect::location("../lr/deposit.php");
	}
	
}
}

}
?>